package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器  章节 和  小结部分
 * </p>
 *
 * @author testjava
 * @since 2021-03-09
 */
@RestController
@RequestMapping("/eduservice/edu-chapter")
//@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    //课程大纲列表（章节 小结）  根据课程id查询
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){

        List<ChapterVo> list=chapterService.getChapterVideoByCourseId(courseId);
        System.out.println(list);
        return R.ok().data("allChapterVideo",list);
    }

    //添加章节  章节 加入数据库
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
    }
    //根据 章节 id查询
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById((chapterId));
        return R.ok().data("chapter",eduChapter);
    }

    //修改章节 根据章节id-->需要先回显数据  根据id查询
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    //删除的方法
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){

        /**
         * 此次删除 只删除没有小结的章节
         */
        boolean flag=chapterService.deleteChapter(chapterId);
        if(flag) {
//            System.out.println("ok======================="+flag);
            return R.ok();
        }else{
//            System.out.println("error======================="+flag);
            return R.error();
        }
    }


}

